package com.thoughtworks.collection;

import sun.reflect.generics.reflectiveObjects.NotImplementedException;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class Add {
    public int getSumOfEvens(int leftBorder, int rightBorder) {
        //求leftBorder 和rightBorder之间的偶数和
        int max = 0;
        int min = 0;
        int num = 0;
        if (leftBorder < rightBorder) {
            min = leftBorder;
            max = rightBorder;
        } else {
            min = rightBorder;
            max = leftBorder;
        }
        for (int i = min; i <= max; i++) {
            if (i % 2 == 0) {
                num = i + num;
            }
        }
        return num;
    }

    public int getSumOfOdds(int leftBorder, int rightBorder) {
        //求leftBorder 和rightBorder之间的奇数和
        int max = 0;
        int min = 0;
        int num = 0;
        if (leftBorder < rightBorder) {
            min = leftBorder;
            max = rightBorder;
        } else {
            min = rightBorder;
            max = leftBorder;
        }
        for (int i = min; i <= max; i++) {
            if (i % 2 != 0) {
                num = i + num;
            }
        }
        return num;
    }

    public int getSumTripleAndAddTwo(List<Integer> arrayList) {
        //求数组中每个元素的3倍加2的和
        int num = 0;
        for (Integer i : arrayList) {
            num = (i * 3 + 2) + num;
        }
        return num;
    }

    public List<Integer> getTripleOfOddAndAddTwo(List<Integer> arrayList) {
        //求数组中奇数元素的3倍加2，偶数元素不变的数组
        List list = new ArrayList();
        for (Integer i : arrayList) {
            if (i % 2 != 0) {
                i = i * 3 + 2;
            }
            list.add(i);
        }
        return list;
    }

    public int getSumOfProcessedOdds(List<Integer> arrayList) {
        //求数组中奇数元素的3倍加5的和
        int num = 0;
        for (Integer i : arrayList) {
            if (i % 2 != 0) {
                num = (i * 3 + 5) + num;
            }

        }
        return num;
    }

    public double getMedianOfEven(List<Integer> arrayList) {
        //求数组中所有偶数组成的数组的中位数
        List<Integer> list = new ArrayList<>();
        double num = 0;
        int a = 1;
        for (Integer i : arrayList) {
            if (i % 2 == 0) {
                list.add(i);
            }
        }
        if (list.size() % 2 == 0) {
            num = (list.get(list.size() / 2 - 1) + list.get(list.size() / 2)) / 2;
        } else {
            num = list.get(list.size() / 2 + 1);
        }

        return num;
    }

    public double getAverageOfEven(List<Integer> arrayList) {
        //求数组中所有偶数的平均数
        int count =0;
        int num = 0;
        double p = 0;
        for (Integer i : arrayList) {
            if (i % 2 == 0) {
                num = i + num;
                count++;
            }
        }
        p = num / count;
        return p;

    }

    public boolean isIncludedInEvenIndex(List<Integer> arrayList, Integer specialElment) {
        // 求数组中所有偶数组成的数组是否包含某特定的数specialElment
        List<Integer> list = new ArrayList<>();
        boolean b = false;
        for (Integer i : arrayList) {
            if (i % 2 == 0) {
                list.add(i);
            }
        }
        for (Integer it : list) {
            if (it.equals(specialElment)) {
                b = true;
            }
        }
        return b;
    }

    public List<Integer> getUnrepeatedFromEvenIndex(List<Integer> arrayList) {
        //找出数组中的所有偶数，从中剔除重复数据并返回
        List<Integer> list = new ArrayList<>();
        for (Integer i : arrayList) {
            if (i % 2 == 0) {
                list.add(i);
            }
        }
        for (int i=list.size()-1;i>0;i--){
            for (int j=0;j<i;j++){
                if (list.get(j).equals(list.get(i))){
                    list.remove(i);
                }
            }
        }
        return list;

    }

    public List<Integer> sortByEvenAndOdd(List<Integer> arrayList) {
        //排序数组 结果使数组偶数在递增在前，奇数递减在后
        List<Integer> list1=new ArrayList<>();
        List<Integer> list2=new ArrayList<>();
        for (Integer i:arrayList){
            if (i%2==0){
                list1.add(i);
            }else {
                list2.add(i);

            }
        }
        Collections.sort(list1);//从小到大
        Collections.sort(list2,Collections.reverseOrder());//从大到校小
        list2.stream().sequential().collect(Collectors.toCollection(() -> list1));
        return list1;
    }

    public List<Integer> getProcessedList(List<Integer> arrayList) {
        // 数组中前一个元素和后一个元素的和的3倍的数组
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < arrayList.size() - 1; i++) {
            int r = (arrayList.get(i) + arrayList.get(i + 1)) * 3;
            list.add(r);
        }
        return list;
    }
}
